package org.zjx.app.action.login;


import org.zjx.analysis.analyze.login.LoginAccountAnalysis;
import org.zjx.command.state.AccountState;
import org.zjx.window.action.Action;
import org.zjx.window.operation.WindowOperation;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.awt.event.KeyEvent;

public class LoginAccountAction extends Action { // 在需要的时候才进行详细分析
    protected LoginAccountAnalysis loginAccountAnalysis;

    public LoginAccountAction(IWindow targetWindow) {
        super(targetWindow, true);
        loginAccountAnalysis = new LoginAccountAnalysis();
        loginAccountAnalysis.analyze(targetWindow.getScreenshot());
    }

    @Override
    protected void doExecute() throws Exception {
        AccountState accountState = targetWindow.getState().getState(AccountState.class);

        if (loginAccountAnalysis.isError(this.targetWindow.getScreenshot())) {
            Rectangle errorButtonRectangle = loginAccountAnalysis.getErrorButtonRectangle();
            Rectangle backButtonRectangle = loginAccountAnalysis.getBackButtonRectangle();

            targetWindow.actions().clickAt(errorButtonRectangle.getCenterX(), errorButtonRectangle.getCenterY())
                    .delay(500).clickAt(backButtonRectangle.getCenterX(), backButtonRectangle.getCenterY());
        }else {
            targetWindow.actions().input(accountState.getAccountName())
                    .delay(300).keyPress(KeyEvent.VK_TAB)
                    .input(accountState.getPassword()).delay(300).keyPress(KeyEvent.VK_ENTER);
        }
        targetWindow.execute(WindowOperation.delay(1000));
    }
}
